const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const cors = require("cors");

const verify = require("./middleware/verify");
const initWS = require("./websocket");

const filesRouter = require("./routers/files");

const loginRouter = require("./routers/login");
const userRouter = require("./routers/user");

const app = express();
const port = 3000;

// 中间件方式加载路由模块
app.use(verify);

// 设置静态文件目录
app.use(express.static(path.join(__dirname, "public")));

// 使用body-parser中间件解析JSON类型的请求体
app.use(bodyParser.json());
// for parsing application/x-www-form-urlencoded
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
);

// 解析表单中的json格式
app.use(express.json());

// 解决跨域
// app.use(cors);

// 模块前缀
app.use("/login", loginRouter);
app.use("/user", userRouter);
app.use("/files", filesRouter);

// 启动websocket
const server = initWS(app);

server.listen(port, () => {
  console.log(`Example app listening on port localhost:${port}`);
});
